﻿/*
 * QWine Queue Machine (Special build for Immigration Office)
 * Copyright (C) 2010 Wish Enterprise Development Team

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Contact Us:
 * E-Mail   : contact@wishenterprise.com
 * Website  : http://www.wishenterprise.com
 * 
 * */

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using QWEntity;
using QWEntity.Local;
using QWRepository;
using QWRepository.Local;

namespace QWControlCenter
{
    /// <summary>
    /// LayananUI : Form
    /// </summary>
    public partial class LayananUI : Form
    {
        private readonly ListViewColumnSorter _lvwColumnSorter;

        /// <summary>
        /// Initializes a new instance of the <see cref="LayananUI"/> class.
        /// </summary>
        public LayananUI()
        {
            InitializeComponent();
            _lvwColumnSorter = new ListViewColumnSorter();
            lsvData.ListViewItemSorter = _lvwColumnSorter;
            cboCopies.SelectedIndex = 0;
            ShowAll();
        }

        /// <summary>
        /// Shows all.
        /// </summary>
        private void ShowAll()
        {
            FillDataGrid(new LayananBase().GetAll());
        }

        /// <summary>
        /// Fills the data grid.
        /// </summary>
        /// <param name="row">The row.</param>
        private void FillDataGrid(IEnumerable<Layanan> row)
        {
            lsvData.Items.Clear();
            foreach (Layanan r in row)
            {
                ListViewItem lvItem = lsvData.Items.Add(r.LayananID, r.LayananID, 0);
                lvItem.SubItems.Add(r.LayananDescription);
                lvItem.SubItems.Add(r.IsLocked ? "Locked" : "Active");
                lvItem.SubItems.Add(r.Copies.ToString().Trim());
            }

            EnvironmentRepository.PasangWarnaAlternatif(lsvData);
        }

        /// <summary>
        /// Resets the data.
        /// </summary>
        private void ResetData()
        {
            ShowAll();
            txtID.Enabled = true;
            txtID.Clear();
            txtID.Focus();
            txtDescription.Clear();
            cboCopies.SelectedIndex = 0;
            chkLocked.Checked = false;
        }

        /// <summary>
        /// Confirms the specified s message.
        /// </summary>
        /// <param name="sMessage">The s message.</param>
        /// <returns></returns>
        private DialogResult Confirm(string sMessage)
        {
            DialogResult dialogResult =
                (MessageBox.Show(sMessage, @"Confirm",
                                 MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question));
            switch (dialogResult)
            {
                case DialogResult.No:
                    ResetData();
                    break;
            }
            return dialogResult;
        }

        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                var r = new Layanan
                            {
                                LayananID = txtID.Text.Trim(),
                                LayananDescription = txtDescription.Text.Trim(),
                                IsLocked = chkLocked.Checked,
                                Copies = cboCopies.SelectedIndex + 1
                            };

                // --- Confirm
                if (Confirm("Do you want to save this data?") != DialogResult.Yes) return;

                DialogResult dlg;
                using (var f = new LogonPresentation())
                {
                    dlg = f.ShowDialog(this);
                }

                if (dlg == DialogResult.OK)
                {
                    IDataAccess<Layanan> db = new LayananBase();
                    if (db.Update(r) == 1) ResetData();
                }
            }
            catch (ArgumentNullException ex)
            {
                MessageBox.Show(ex.Message, @"Warning!",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, @"Warning!",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        /// <summary>
        /// Handles the Validated event of the txtID control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtID_Validated(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtID.Text.Trim())) return;

            Layanan r = new LayananBase().GetById(txtID.Text.Trim());

            try
            {
                txtDescription.Text = r.LayananDescription;
                chkLocked.Checked = r.IsLocked;
                cboCopies.SelectedIndex = r.Copies - 1;
                txtID.Enabled = false;
            }
            catch (NullReferenceException)
            {
                MessageBox.Show(@"ID tidak dikenal !", @"Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ResetData();
            }
        }

        /// <summary>
        /// Handles the Click event of the btnClear control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            ResetData();
        }

        /// <summary>
        /// Handles the ItemActivate event of the lsvData control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void lsvData_ItemActivate(object sender, EventArgs e)
        {
            if ((lsvData.Items.Count <= 0) | (lsvData.SelectedItems.Count <= 0)) return;
            txtID.Text = lsvData.SelectedItems[0].Text.Trim();
            txtID_Validated(this, null);
        }

        /// <summary>
        /// Handles the ColumnClick event of the lsvData control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.ColumnClickEventArgs"/> instance containing the event data.</param>
        private void lsvData_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column == _lvwColumnSorter.SortColumn)
            {
                _lvwColumnSorter.Order = _lvwColumnSorter.Order == SortOrder.Ascending
                                             ? SortOrder.Descending
                                             : SortOrder.Ascending;
            }
            else
            {
                _lvwColumnSorter.SortColumn = e.Column;
                _lvwColumnSorter.Order = SortOrder.Ascending;
            }

            lsvData.Sort();
            EnvironmentRepository.PasangWarnaAlternatif(lsvData);
        }

        /// <summary>
        /// Handles the TextChanged event of the txtID control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtID_TextChanged(object sender, EventArgs e)
        {
            ((TextBox)sender).Text = EnvironmentRepository.SuppressChar(((TextBox)sender).Text);
        }

        /// <summary>
        /// Handles the TextChanged event of the txtDescription control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtDescription_TextChanged(object sender, EventArgs e)
        {
            ((TextBox)sender).Text = EnvironmentRepository.SuppressChar(((TextBox)sender).Text);
        }
    }
}